/*
 * line.cpp
 *
 *  Created on: Jan 18, 2012
 *      Author: beebe
 */

#include <iostream>
#include <string.h>
#include "line.h"
using namespace std;

class line{
private:
	int field;
public:
	bool notEmpty;
	char extractedField[1024];
	char * originalLine;
	line(char * originalLine, int field): originalLine(originalLine), field(field){
		notEmpty = false;
		extractField(extractedField,field,originalLine);
	}
	void setOriginalLine(char * originalLine){
		this->originalLine = originalLine;
	}
	void setField(int field){
		this->field = field;
	}
	void extractField(char dest[], int field, char * line ){
		//Loop through char * line and copy the requested field to the dest *
		int currentField=1;
		//bNewField will be true if a new field is found and will remain true untill the first white space


		bool bNewField = false;
		char * pntStart = NULL;
		char * pntEnd = line+strlen(line);

		while(*line != '\0'){
			if(isspace(*line) && pntStart!= NULL){//If we found the field we want
				pntEnd = line;
				break;
			}else if(isspace(*line) && bNewField == true){//If we are exiting a field
				currentField++;
				bNewField = false;
				continue;
			} else if(isspace(*line)){//If we are inbetween fields
				line++;
				continue;
			}
			//If we find the field, save away the starting pointer
			bNewField = true;
			if(currentField == field && pntStart == NULL){
				pntStart = line;
			}
			line++;
		}
		//Copy field to supplied destination pointer
		if(pntStart != NULL && pntEnd !=NULL){
			notEmpty = true;
			strncpy(dest,pntStart,(pntEnd-pntStart));
		}
	}

};


